package com.motong.gongdan.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motong.gongdan.config.ResponseData;
import com.motong.gongdan.config.auth.UserLoginToken;
import com.motong.gongdan.entity.DeviceLampType;
import com.motong.gongdan.entity.LampType;
import com.motong.gongdan.entity.User;
import com.motong.gongdan.service.DeviceLampTypeService;
import com.motong.gongdan.service.LampTypeService;
import com.motong.gongdan.service.UserService;
import com.motong.gongdan.util.LoginUtil;
import com.motong.gongdan.vo.system.deviceLampType.query.DetailListQuery;
import com.motong.gongdan.vo.system.deviceLampType.query.DeviceLampTypeEditQuery;
import com.motong.gongdan.vo.system.deviceLampType.query.DeviceLampTypeListQuery;
import com.motong.gongdan.vo.system.deviceLampType.vo.DeviceLampTypeListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "联络灯到人配置")
@RestController
@RequestMapping("/sys/deviceLampType")
public class DeviceLampTypeController {

    @Resource
    private DeviceLampTypeService deviceLampTypeService;

    @Resource
    private LampTypeService lampTypeService;

    @Resource
    private UserService userService;


    @ApiOperation("获取联络灯到人配置列表")
    @PostMapping("/getList")
    @UserLoginToken
    public ResponseData<List<DeviceLampTypeListVo>> getList(@RequestBody(required = false) DeviceLampTypeListQuery query) {
        query = Optional.ofNullable(query).orElse(new DeviceLampTypeListQuery());
        Page<DeviceLampTypeListVo> data = deviceLampTypeService.getList(new Page<>(query.getPage(), query.getLimit()), query);
        return ResponseData.success(data.getRecords(), data.getTotal());
    }

    @ApiOperation("获取联络灯到人配置详情列表")
    @PostMapping("/getDetailList")
    @UserLoginToken
    public ResponseData<List<DeviceLampType>> getDetailList(@RequestBody DetailListQuery query) {
        List<DeviceLampType> data = deviceLampTypeService.list(new LambdaQueryWrapper<DeviceLampType>().eq(DeviceLampType::getDeviceId, query.getDeviceId()));
        if (!CollectionUtils.isEmpty(data)){
            data.forEach(i->{
                LampType lampType = lampTypeService.getById(i.getLampTypeId());
                if (Objects.nonNull(lampType)){
                    i.setLampTypeName(lampType.getName());
                }
            });

        }
        return ResponseData.success(data);
    }

    @ApiOperation("编辑联络灯到人配置")
    @PostMapping("/edit")
    @UserLoginToken
    @Transactional
    public ResponseData<String> edit(@RequestBody DeviceLampTypeEditQuery query) {
        if (!CollectionUtils.isEmpty(query.getRemoveIds())) {
            deviceLampTypeService.removeByIds(query.getRemoveIds());
        }
        if (!CollectionUtils.isEmpty(query.getDeviceLampTypes())) {
            query.getDeviceLampTypes().forEach(i -> {
                boolean isUpdate = Objects.nonNull(i.getId());
                i.setUpdateTime(new Date()).setUpdateUser(LoginUtil.getLoginUserId());
                if (StringUtils.hasLength(i.getInfoUser())) {
                    List<String> userIds = Arrays.asList(i.getInfoUser().split(","));
                    String userNames = userService.listByIds(userIds).stream().map(User::getName).collect(Collectors.joining(","));
                    i.setInfoUserName(userNames);
                } else {
                    i.setInfoUserName("");
                }
                if (!isUpdate) {
                    i.setCreateTime(new Date()).setCreateUser(LoginUtil.getLoginUserId());
                }
            });
        }
        deviceLampTypeService.saveOrUpdateBatch(query.getDeviceLampTypes());
        return ResponseData.success("编辑成功!");
    }

//    @ApiOperation("删除联络灯到人配置")
//    @PostMapping("/del")
//    @UserLoginToken
//    public ResponseData<String> del(@RequestBody IdValueQuery query) {
//        if (StringUtils.hasLength(query.getIds())) {
//            deviceLampTypeService.removeByIds(query.getIdList());
//        }
//        return ResponseData.success("删除成功!");
//    }


}
